package cn.hhhy.controller;

import cn.hhhy.pojo.PageResult;
import cn.hhhy.pojo.QueryPageBean;
import cn.hhhy.pojo.Result;
import cn.hhhy.pojo.User;
import cn.hhhy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Result login(@RequestBody User user) {
        User u;
        u = userService.login(user);
        if (u != null) {
            return new Result(true,"登录成功",u);
        }
        return new Result(false,"请检查用户名或者密码");
    }
    // 分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) {
        return userService.findPage(pageBean);
    }
    // 根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            User u = userService.findById(id);
            return new Result(true,"查询成功",u);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    // 添加用户
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> users) {
        return userService.add(users);
    }
    // 删除用户
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            userService.delete(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
    // 修改用户
    @RequestMapping("/update")
    public Result update(@RequestBody User user) {
        try {
            userService.update(user);
            return new Result(true,"更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"更新失败");
        }

    }

}
